/**
 * 
 */
var rowNumber;
	$(function() {
		$('#projectApply_table').datagrid(
				{
					url : 'admin/queryAdprojectApplyInfo.do?pageSize=10',
					collapsible : true,
					width : '80%',
					height : '450px',
					iconCls : 'icon-user',
					fitColumns : true,
					pagination : true,
					rownumbers : true,
					singleSelect : true,
					checkOnSelect : false,
					selectOnCheck : false,
					loadMsg : '正在加载数据',
					loadFilter : function(data) {
						for (var i = 0; i < data.rows.length; i++) {
							if (data.rows[i].paStatus == 1) {
								data.rows[i].paStatus = '待审核';
							}
							if (data.rows[i].paStatus == 2) {
								data.rows[i].paStatus = '审核通过';
							}
							if (data.rows[i].paStatus == 3) {
								data.rows[i].paStatus = '审核未通过';
							}
						}
						return data;
					},
					onClickRow : function(rowIndex, rowData) {
						rowNumber=rowIndex;
					},
					onLoadSuccess:function(data){
						$('#projectApply_table').datagrid('selectRow',rowNumber);
					},
					onDblClickRow : function(rowIndex, rowData) {
						//layer.msg("double click");
					},
					toolbar : '#toolbar',
					columns : [ [ {
						field : 'paTitle',
						title : '申请标题',
						width : 50,
						align : 'center'
					}, {
						field : 'userName',
						title : '申请人',
						width : 50,
						align : 'center'
					}, {
						field : 'userTelephone',
						title : '联系方式',
						width : 50,
						align : 'center'
					}, {
						field : 'paDescription',
						title : '申请描述',
						width : 50,
						align : 'center'
					}, {
						field : 'paStatus',
						title : '申请状态',
						width : 50,
						align : 'center'
					}, {
						field : 'paTime',
						title : '申请时间',
						width : 50,
						align : 'center'
					}, {
						field : 'paVerifyTime',
						title : '审核时间',
						width : 50,
						align : 'center'
					} ] ]
				});

		//设置分页控件  
		var p = $('#projectApply_table').datagrid('getPager');
		$(p).pagination({
			pageSize : 10,//每页显示的记录条数，默认为10  
			pageList : [ 5, 10, 20, 30, 40, 50 ],//可以设置每页记录条数的列表  
			beforePageText : '第',//页数文本框前显示的汉字  
			afterPageText : '页    共 {pages} 页',
			displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
			onSelectPage : function(pageNumber, pageSize) {
				//layer.msg("pageNumber="+pageNumber+"pageSize="+pageSize)
				var datas = {
					paStatus : $('#spaStatus').combobox('getValue'),
					userName : $("#suserName").textbox('getValue'),
					userTelephone : $('#suserTelephone').textbox('getValue'),
					currentPage : (pageNumber - 1) * pageSize,
					pageSize : pageSize
				}
				loadData(datas);
			},
			onRefresh : function(pageNumber, pageSize) {
				var datas = {
					paStatus : $('#spaStatus').combobox('getValue'),
					userName : $("#suserName").textbox('getValue'),
					userTelephone : $('#suserTelephone').textbox('getValue'),
					currentPage : (pageNumber - 1) * pageSize,
					pageSize : pageSize
				}
				loadData(datas);
			}
		});
	});
	$(function(){
		$('#update').bind('click',function(){
			var row = $('#projectApply_table').datagrid('getSelected');
			if (row) {
				if (row.paStatus == "审核通过") {
					layer.msg('审核已通过,禁止修改');
				} else {
					$('#projectApplydlg').form('clear');
					$('#projectApplydlg').dialog('open').dialog("setTitle", "审核申请");
					$('#projectApplyfm').form('load', row);
					$('#paTitle').textbox('readonly',true);
					$('#userName').textbox('readonly',true);
					$('#userTelephone').textbox('readonly',true);
					$('#paDescription').textbox('readonly',true);
					$('#paTime').textbox('readonly',true);
					$('#saveUrl').val('admin/updateAdprojectApplyInfo.do?paId='
									+ row.paId+'&paUserId='+row.paUserId);
				}
			} else {
				layer.msg('请选中一行!');
			}
		});
	});
	//search btn listener
	$(function(){
		$('#searchAdprojectapply').click(
				function() {
					var pageSize = $('#projectApply_table').datagrid('getPager').data("pagination").options.pageSize;
					var datas = {
						paStatus : $('#spaStatus').combobox('getValue'),
						userName : $("#suserName").textbox('getValue'),
						userTelephone : $('#suserTelephone').textbox('getValue'),
						pageSize : pageSize
					}
					loadData(datas);
				});
	});
	//reset btn listener
	$(function(){
		$('#resetAdprojectapply').click(
				function() {
					$('#spaStatus').combobox('setValue','');
					$("#suserName").textbox('setValue','');
					$('#suserTelephone').textbox('setValue','');
					var pageSize = $('#projectApply_table').datagrid('getPager')
							.data("pagination").options.pageSize;
					var datas = {
						pageSize : pageSize
					}
					loadData(datas);
				});
	});
	//load data function
	var loadData = function(datas) {
		$.ajax({
			url : 'admin/queryAdprojectApplyInfo.do',
			type : "POST",
			dataType : "json",
			data : datas,
			success : function(param) {
				$('#projectApply_table').datagrid('loadData', param);
			}
		});
	}

	//save btn listener
	function projectApplySave() {
		var pageSize = $('#projectApply_table').datagrid('getPager').data(
		"pagination").options.pageSize;
		var pageNumber =  $('#projectApply_table').datagrid('getPager').data(
		"pagination").options.pageNumber;
		var datas={
				currentPage : (pageNumber - 1) * pageSize,
				pageSize:pageSize
				};
		$.ajax({
			url : $("#saveUrl").val(),
			type : "POST",
			dataType : "json",
			data : {
				spaStatus : $("#paStatus").combobox('getValue'),
			},
			success : function(result) {
				if (result.result == 0) {
					layer.msg(result.tips);
				} else {
					layer.msg(result.tips);
					$('#projectApplydlg').dialog('close'); // close the dialog
					$('#projectApply_table').datagrid('reload',datas); // reload the data
				}
			}
		});
	}
	//Cancel btn listeners
	function projectApplyCancel() {
		$('#projectApplyfm').form('clear');
		$('#projectApplydlg').dialog('close');
	}